<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>17-3 数组排序：快速排序</title>
</head>

<body>
  <script>
    Array.prototype.quick = function quick() {
      let _this = this;

      if (_this.length <= 1) {
        return _this;
      }

      let middleIndex = Math.floor(_this.length / 2),
        middleValue = _this.splice(middleIndex, 1)[0];

      let arrLeft = [],
        arrRight = [];
      for (let i = 0; i < _this.length; i++) {
        let item = _this[i];
        item < middleValue ? arrLeft.push(item) : arrRight.push(item);
      }

      return quick.call(arrLeft).concat(middleValue, quick.call(arrRight));
    };

    let arr = [12, 34, 22, 54, 1];
    arr = arr.quick();
    console.log(arr);
  </script>
</body>

</html>